package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSIndexPath;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIEdgeInsets;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIScreen;
import czh.cocoa.ui.UIView;

import static czh.cocoa.ui.impl.UITableViewCell.*;
import static java.lang.Math.round;

public class UIPickerView extends UIView implements UITableViewDataSource,UITableViewDelegate {

	private final static float PICKER_PADDING_V = 20;
	private final static float PICKER_PADDING_H = 30;
	private final static float SELECTION_HEIGHT = 85;
	private final static float SELECTION_INSET = 150;
	
	private UITableView[] mTableViews;
	
	//private float mSelectionHeight;
	
	private UIPickerViewDataSource mDataSource;
	
	private UIPickerViewDelegate mDelegate;
	
	private float mFrameRatio;
	
	public UIPickerView() {
		
	}
	
	public UIPickerView(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		
		UIImage background = UIImage.imageNamed(NSTR("cocoa/picker_frame"));
		CGSize bgsize = background.size();
		
		float frame_width = UIScreen.mainScreen().bounds().size.width;
		float frame_height = frame_width * (bgsize.height / bgsize.width);
		CGRect frame = CGRectMake(0, 0, frame_width, frame_height);
		setFrame(frame); 
		
		float ratio = frame_width / bgsize.width;
		
		UIImageView bgview = new UIImageView(frame);
		bgview.setImage(background);
		bgview.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
		addSubview(bgview);
		
//		UITableView table_view = new UITableView();
//		float padding = PICKER_PADDING * ratio;
//		table_view.setFrame(frame.inset(padding, padding));
//		table_view.setDataSource(this);
//		table_view.setDelegate(this);
//		table_view.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
//		table_view.setRowHeight(SELECTION_HEIGHT * ratio);
//		float inset = SELECTION_INSET * ratio;
//		table_view.setContentInset(UIEdgeInsetsMake(inset, 0, inset, 0));
//		table_view.setShowsVerticalScrollIndicator(false);
//		table_view.setSeparatorStyle(UITableViewCellSeparatorStyleNone);
//		addSubview(table_view);
		
		//mTableView = table_view;
		mFrameRatio = ratio;
		mTableViews = new UITableView[0];
	}
	
	public void setDataSource(UIPickerViewDataSource dataSource) {
		mDataSource = dataSource;
	}
	
	public UIPickerViewDataSource dataSource() {
		return mDataSource;
	}
	
	public void setDelegate(UIPickerViewDelegate delegate) {
		mDelegate = delegate;
	}
	
	public UIPickerViewDelegate delegate() {
		return mDelegate;
	}

	public void reloadAllComponents() {
		for (UITableView view : mTableViews) {
			view.removeFromSuperview();
		}
		if (mDataSource != null) { 
			int components = mDataSource.numberOfComponentsInPickerView(this);
			mTableViews = new UITableView[components];
			
			final float ratio = mFrameRatio;
			CGRect picker_frame = bounds();
			picker_frame = picker_frame.inset(PICKER_PADDING_H * ratio, PICKER_PADDING_V * ratio);
			
			//if (components > 1) {
				UIImage divider = UIImage.imageNamed(NSTR("cocoa/picker_divider")); 
				float divider_width = divider.size().width;
				float divider_height = divider.size().height;
				float component_width = components > 1 ? (picker_frame.size.width - 
										divider_width * (components - 1)) / components : 
										picker_frame.size.width;
				float component_height = picker_frame.size.height;
				float component_top = picker_frame.origin.y;
				
				float offx = picker_frame.origin.x;
				for (int i = 0; i < components; i++) {
					CGRect frame = CGRectMake(offx, component_top, component_width, component_height);
					UITableView table_view = new UITableView(frame);
					table_view.setDataSource(this);
					table_view.setDelegate(this);
					table_view.setAutoresizingMask(UIViewAutoresizingFlexibleSize);
					table_view.setRowHeight(SELECTION_HEIGHT * ratio);
					float inset = SELECTION_INSET * ratio;
					table_view.setContentInset(UIEdgeInsetsMake(inset, 0, inset, 0));
					table_view.setShowsVerticalScrollIndicator(false);
					table_view.setSeparatorStyle(UITableViewCellSeparatorStyleNone);
					addSubview(table_view);
					
					mTableViews[i] = table_view;
					
					table_view.reloadData();
					UIEdgeInsets insets = table_view.contentInset();
					table_view.setContentOffset(CGPointMake(0, -insets.top)); 
					
					offx += component_width;
					
					if (i != components - 1) {
						UIImageView divider_view = new UIImageView(divider);
						divider_view.setFrame(CGRectMake(offx, component_top + 0.5f, 
								divider_width * ratio, divider_height * ratio));
						addSubview(divider_view);
						
						offx += divider_width;
					}

				}
			//}
		}
		 
		
//		mTableView.reloadData();
//		UIEdgeInsets insets = mTableView.contentInset();
//		mTableView.setContentOffset(CGPointMake(0, -insets.top)); 
	}
	
	public void selectRowInComponentAnimated(int row, int component, boolean animated) {
		UITableView tableView = mTableViews[component];
		UIEdgeInsets insets = tableView.contentInset();
		float offy = row * tableView.rowHeight() - insets.top;
		tableView.setContentOffsetAnimated(CGPointMake(0, offy), animated);
	}
	
	private void adjustPickerSelection(UITableView tableView) {
		float offy = tableView.contentOffset().y;
		UIEdgeInsets insets = tableView.contentInset();
		int row = round((offy + insets.top) / tableView.rowHeight());
		offy = row * tableView.rowHeight() - insets.top;
		tableView.setContentOffsetAnimated(CGPointMake(0, offy), true); 
		
		if (mDelegate != null) {
			int component = componentIndexForTableView(tableView);
			mDelegate.pickerViewDidSelectRowInComponent(this, row, component);
		}
	}
	
	private int componentIndexForTableView(UITableView tableView) {
		int compoment = 0;
		for (int i = 0; i < mTableViews.length; i++) {
			if (mTableViews[i] == tableView) { 
				compoment = i;
				break;
			}
		}
		return compoment;
	}
	
	@Override
	public int numberOfSectionsInTableView(UITableView tableView) {
		return 1;
	}

	@Override
	public int tableViewNumberOfRowsInSection(UITableView tableView, int section) {
		if (mDataSource != null) {
			int component = componentIndexForTableView(tableView);
			return mDataSource.pickerViewNumberOfRowsInComponent(this, component);
		}
		return 0;
	}

	@Override
	public UITableViewCell tableViewCellForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		NSString identifier = NSTR("picker_cell");
		UITableViewCell cell = tableView.dequeueReusableCellWithIdentifier(identifier);
		if (cell == null) {
			cell = new UITableViewCell(UITableViewCellStyleDefault, identifier);
			cell.setSelectionStyle(UITableViewCellSelectionStyleNone); 
		}
		
		NSString title;
		if (mDelegate != null) { 
			int row = indexPath.row();
			int component = componentIndexForTableView(tableView);
			title = mDelegate.pickerViewTitleForRowForComponent(this, row, component);
		} else {
			title = NSTR("?");
		}
		
		cell.textLabel().setText(title);
		
		return cell;
	}

	@Override
	public NSString tableViewTitleForHeaderInSection(UITableView tableView, int section) {
		return null;
	}

	@Override
	public UIView viewForZoomingInScrollView(UIScrollView scrollView) {
		return null;
	}

	@Override
	public void scrollViewDidScroll(UIScrollView scrollView) {
		
	}

	@Override
	public void scrollViewDidEndDraggingWillDecelerate(UIScrollView scrollView, boolean decelerate) {
		if (!decelerate) {
			adjustPickerSelection((UITableView)scrollView);
		}
	}

	@Override
	public void scrollViewWillBeginDecelerating(UIScrollView scrollView) {
		
	}

	@Override
	public void scrollViewDidEndDecelerating(UIScrollView scrollView) {
		adjustPickerSelection((UITableView)scrollView);
	}

	@Override
	public float tableViewHeightForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		return 0;
	}

	@Override
	public float tableViewHeightForHeaderInSection(UITableView tableView, int section) {
		return 0;
	}

	@Override
	public UIView tableViewViewForHeaderInSection(UITableView tableView, int section) {
		return null;
	}

	@Override
	public void tableViewDidSelectRowAtIndexPath(UITableView tableView, NSIndexPath indexPath) {
		
	}
	
}
